/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer.jxmap.map;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Timon Veenstra
 */
public class MapMouseHandler implements MouseListener, MouseMotionListener{
    
    private static final Logger LOGGER = Logger.getAnonymousLogger();
    
//    static{
//        LOGGER.setLevel(Level.ALL);
//    }
    
    private final Comparator<MapMouseListener> comparator = new Comparator<MapMouseListener>() {
        @Override
        public int compare(MapMouseListener o1, MapMouseListener o2) {
            if (o1.getGroup().equals(o2.getGroup())){
                return (o1.getId() == o2.getId())?0:(o1.getId() < o2.getId())?-1:1;
            }else{
                return (o1.getGroup().getValue() == o2.getGroup().getValue())?0:(o1.getGroup().getValue() > o2.getGroup().getValue())?-1:1;
            }
        }
    };
    
    private final Component rootPanel;
    private final SortedSet<MapMouseListener> listeners = new TreeSet<MapMouseListener>(comparator);

    public MapMouseHandler(Component rootPanel) {
        this.rootPanel = rootPanel;
    }
    
    public void addMapMouseListener(MapMouseListener mapMouseListener){
        listeners.add(mapMouseListener);
    }
    
    public void removeMapMouseListener(MapMouseListener mapMouseListener){
        listeners.remove(mapMouseListener);
    }
    
    /**
     * dispatch the event to the listeners and the map panel
     * 
     * @param e 
     */
    private void dispatch(MouseEvent e){
        LISTENERS:
        for (MapMouseListener listener:listeners){
            trace("Dispatching to:");
            trace(listener.getComponent().toString());
            trace(listener.getId()+"");
            trace(listener.getGroup().name());
            listener.getComponent().dispatchEvent(e);
            if (e.isConsumed() || e.isPopupTrigger()){
                trace("is consumed");
                break LISTENERS;
            }
        }
        if (!e.isConsumed() || e.isPopupTrigger()){
            trace("dispatching to the root panel");
            rootPanel.dispatchEvent(e);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        trace("mouseClicked");
        dispatch(e);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        trace("mousePressed");
        dispatch(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        trace("mouseReleased");
        dispatch(e);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        trace("mouseEntered");
        dispatch(e);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        trace("mouseExited");
        dispatch(e);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        trace("mouseDragged");
        dispatch(e);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        trace("mouseMoved");
        dispatch(e);
    }
    
    private void trace(String msg){
        if (LOGGER.isLoggable(Level.FINEST)){
            LOGGER.finest(msg);
        }
    }
    
}
